home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE23 / SYSTEM / DELIMIT.PAS next >
Pascal/Delphi Source File  |  1997-05-17  |  2KB  |  94 lines

  1. unit Delimit;
  2.  
  3. interface
  4.  
  5. uses SysUtils, Classes;
  6.  
  7. type
  8.     TDelimitedString = class (TObject)
  9.     private
  10.         fStr: String;
  11.         fList: TStringList;
  12.         fDelimiter: Char;
  13.         procedure ParseString;
  14.         procedure SetText (const NewStr: String);
  15.         function GetFieldCount: Integer;
  16.         function GetField (Index: Integer): String;
  17.         procedure SetDelimiter (Delim: Char);
  18.     public
  19.         constructor Create;
  20.         constructor CreateAssign (const NewStr: String);
  21.         destructor Destroy; override;
  22.         property FieldCount: Integer read GetFieldCount;
  23.         property Text: String read fText write SetText;
  24.         property Delimiter: Char read fDelimiter write SetDelimiter;
  25.         property Field [Index: Integer]: String read GetField; default;
  26.     end;
  27.  
  28. implementation
  29.  
  30. constructor TDelimitedString.Create;
  31. begin
  32.     Inherited Create;
  33.     fDelimiter := ',';
  34.     fList := TStringList.Create;
  35. end;
  36.  
  37. constructor TDelimitedString.CreateAssign (const NewStr: String);
  38. begin
  39.     Self.Create;
  40.     Assign (NewStr);
  41. end;
  42.  
  43. destructor TDelimitedString.Destroy;
  44. begin
  45.     fList.Free;
  46.     Inherited Destroy;
  47. end;
  48.  
  49. procedure TDelimitedString.ParseString;
  50. var
  51.     i: Integer;
  52.     buff: String;
  53. begin
  54.     fList.Clear;
  55.     buff := fStr;
  56.     while Length (buff) > 0 do
  57.     begin
  58.         i := Pos (Delimiter, buff);
  59.         if i = 0 then i := Length (buff) + 1;
  60.         fList.Add (Copy (buff, 1, i - 1));
  61.         Delete (buff, 1, i);
  62.     end;
  63. end;
  64.  
  65. procedure TDelimitedString.Assign (const NewStr: String);
  66. begin
  67.     fStr := NewStr;
  68.     ParseString;
  69. end;
  70.  
  71. function TDelimitedString.GetFieldCount: Integer;
  72. begin
  73.     Result := fList.Count;
  74. end;
  75.  
  76. function TDelimitedString.GetField (Index: Integer): String;
  77. begin
  78.     Result := '';
  79.     if (Index >= 0) and (Index < fList.Count) then
  80.         Result := fList.Strings [Index];
  81. end;
  82.  
  83. procedure TDelimitedString.SetDelimiter (Delim: Char);
  84. begin
  85.     if fDelimiter <> Delim then
  86.     begin
  87.         fDelimiter := Delim;
  88.         ParseString;
  89.     end;
  90. end;
  91.  
  92. end.
  93.  
  94.